$(document).ready(function() {
	// 初始化表格
	initTable();
});

// 初始化table
function initTable() {
	$('#tt')
			.datagrid(
					{
						width : backendCommon.getWidth(0.96),
						loadMsg : "数据装载中....",
						url : mainDomain+'/task/findTaskInfo.action',
						nowrap : false,
						fitColumns : true,
						striped : true,
						rownumbers : true,
						singleSelect : true,
						pagination : true,
						pageSize : 10,
						queryParams : {
							'operator':1,
							'rn' : Math.random()
						},
						columns : [ [
								{
									field : 'jobName',
									title : 'job名称',
									width : $(this).width() * 0.08,
									align : 'center'
								},
								{
									field : 'jobStatus',
									title : '状态',
									width : $(this).width() * 0.02,
									align : 'center',
									formatter : function(value, rec, index) {
										var str = "";
										if(!value){
											str="停止";
										}
										return str;
									}
								},
								{
									field : 'triggerStatus',
									title : '触发器状态',
									width : $(this).width() * 0.03,
									align : 'center',
									formatter : function(value, rec, index) {
										var str="";
										if(value){
											if(value != 'NORMAL'){
												str +="<b style='color: red'>"+value+"</b>";
											}else{
												str = value;
											}
										}
										return str;
									}
								},
								{
									field : 'previousFireTime',
									title : '上次执行时间',
									width : $(this).width() * 0.08,
									align : 'center',
									formatter : function(value, rec, index) {
										return backendCommon
										.dateTimeToLocalString(value);
									}
								},
								{
									field : 'previousCost',
									title : '上次执行耗时(毫秒)',
									width : $(this).width() * 0.04,
									align : 'center'
								},
								{
									field : 'previousFireType',
									title : '上次触发方式',
									width : $(this).width() * 0.03,
									align : 'center',
									formatter : function(value, rec, index) {
										var str ="";
										if(value == 0){
											str="自动";
										}else if(value == 1){
											str="手动";
										}else{
											str="未知";
										}
										return str;
									}
								},
								{
									field : 'nextFireTime',
									title : '下次执行时间',
									width : $(this).width() * 0.08,
									align : 'center',
									formatter : function(value, rec, index) {
										return backendCommon
										.dateTimeToLocalString(value);
									}
								},
								{
									field : 'description',
									title : '触发器描述',
									width : $(this).width() * 0.12,
									align : 'center'
								},
								{
									field : 'opt',
									title : '操作',
									width : $(this).width() * 0.05,
									align : 'center',
									formatter : function(value, rec, index) {
										var str = "";
										if(rec.triggerStatus == "PAUSED"){
											str += '<div><a  index="'+index+'" href="javascript:void(0);" onclick="resumeJob('+ index + ');">继续</a></div>';
										}else{
											str += '<div><a  index="'+index+'" href="javascript:void(0);" onclick="pauseJob('+ index + ');">暂停</a></div>';
										}
										str += '<div><a  index="'+index+'" href="javascript:void(0);" onclick="triggerJob('+ index + ');">立刻执行</a></div>';
//										str += '<div><a  index="'+index+'" href="javascript:void(0);" onclick="showHistory('+ index + ');">历史记录</a></div>';
										return str;
									}
								} ] ]
					});
}

function resumeJob(index){
	var datas = $('#tt').datagrid('getData').rows;
	var jobName = datas[index].jobName;
	var groupName = datas[index].jobGroup;
	$.post(mainDomain+'/task/resumeJob.action',{'jobName':jobName,'groupName':groupName,'operator':1,});
	$("#tt").datagrid('reload');
	
}

function pauseJob(index){
	var datas = $('#tt').datagrid('getData').rows;
	var jobName = datas[index].jobName;
	var groupName = datas[index].jobGroup;
	$.post(mainDomain+'/task/pauseJob.action',{'jobName':jobName,'groupName':groupName,'operator':1,});
	$("#tt").datagrid('reload');
}

function triggerJob(index){
	var datas = $('#tt').datagrid('getData').rows;
	var jobName = datas[index].jobName;
	var groupName = datas[index].jobGroup;
	$.post(mainDomain+'/task/triggerJob.action',{'jobName':jobName,'groupName':groupName,'operator':1,});
	$("#tt").datagrid('reload');
}

function showHistory(index){
	var datas = $('#tt').datagrid('getData').rows;
	var jobName = datas[index].jobName;
	var groupName = datas[index].jobGroup;
	window.open(mainDomain+'/task/history.action?jobName='+jobName+'&groupName='+groupName+'&operator='+1,"_blank");
}

